<?php
include_once '../../include/dbconf.php';
class Product_info{
	var $product_small_picture;
	var $product_big_picture;
	var $product_add_time;
	var $product_edit_time;
	var $show_as_scroll;
	var $show_home_page;
	var $product_catlog_id;
	var $product_content;
	var $product_name;
	var $product_desc;
	var $id;
	function getProductSmallPicture(){
		return  $this->product_small_picture;
	}
};

class pageInfo{
	var $items;
	var $totalCount;
}

/**
 * 
 * Enter description here ...
 * @param   $id
 */
function list_product_by_id($id){
	global $db;
	$sql="select a.*,b.product_content  from product    a inner join product_content b on  a.id=$id and a.id=b.id ;";
	 
	$ret=$db->get_results($sql);
	return  $ret;
}

/**
 * 
 * Enter description here ...
 * @param   $pageIndex
 * @param   $pageSize
 */
function list_product($pageIndex,$pageSize){
	$offset=($pageIndex-1)* $pageSize;
	$sql="select * from product limit $offset ,  $pageSize";
	global  $db;
	$cSql="select count(0) totalCount from product";
	 
	//echo $sql;
	$list=$db->get_results($cSql,	true);
	if($list!=false){
		$totalCount= $list[0]->totalCount ;
		if($totalCount>0){
			$plist=$db->get_results($sql,true);
			 
			if($plist!=false){
				$pInfo=new pageInfo();
				$pInfo->items=$plist;
				$pInfo->totalCount=$totalCount;		
			 	 
				return  $pInfo;
			}
		}
		return  false;
	}
	 return  false;
}


/**
 *
 * Enter description here ...
 *
 */
function product_modify($product_info){
	$sql ="update  product set  product_name ='$product_info->product_name',
	  product_catlog_id =$product_info->product_catlog_id,
	  product_desc ='$product_info->product_desc',	  
	  product_edit_time ='$product_info->product_edit_time', 
	  product_big_picture='$product_info->product_big_picture', 
	  product_small_picture ='$product_info->product_small_picture',
	  show_home_page = $product_info->show_home_page ,
	  show_as_scroll =$product_info->show_as_scroll 
	  where id=$product_info->id
	  ";
	global $db;
	$ret=$db->query($sql);
	if($ret>=0){
		$sql=" update   product_content  set product_content='$product_info->product_content' where id=$product_info->id";
		echo  $sql;
		$ret2=$db->query($sql);				 
		if(ret2>=0){
			return  true;
		}return  false;
	}
	return  false;

}

/**
 * product delete
 * Enter description here ...
 * @param unknown_type $id
 */
function product_delete($id){
	global $db;
	$ret=$db->query("delete from product where id=$id");
	if($ret>0){
		$ret2=$db->query("delete from product_content where id=$id");
		if(ret2>0){
			return  true;
		}return  false;
	}
	return  false;
}

/**
 * product add
 * Enter description here ...
 * @param   $product_info
 */

function  product_add($product_info ){
	global $db;
	$sql="insert into  product(
	  product_name,
	  product_catlog_id ,
	  product_desc ,
	  product_add_time ,
	  product_edit_time ,
	  product_big_picture, 
	  product_small_picture ,
	  show_home_page ,
	  show_as_scroll) values(
	  '$product_info->product_name',
	  $product_info->product_catlog_id,
	  '$product_info->product_desc',
	  '$product_info->product_add_time',
	  '$product_info->product_edit_time',
	  '$product_info->product_big_picture',
	  '$product_info->product_small_picture',
	  $product_info->show_home_page ,
	  $product_info->show_as_scroll)
	  ";

	  $ret=$db->query($sql);
	  if($ret>0){
	  	$ret=$db->get_results("SELECT LAST_INSERT_ID() lastId;",true);

	  	if($ret!=false){
	  		$id=$ret[0]->lastId;
	  		$sql=" insert into product_content values($id,'$product_info->product_content'); ";


	  		$ret=$db->query($sql);
	  		if($ret>0){
	  			return  true;
	  		}
	  	}
	  }
	  return  false;

}
?>